//
//  RecommendNewMusicViewController.m
//  MusicPlayer
//
//  Created by dlios on 15-5-23.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "RecommendNewMusicViewController.h"
#import "ReNewMusicChineseViewController.h"
#import "ReNewMusicTableViewCell.h"
@interface RecommendNewMusicViewController ()<UIScrollViewDelegate>
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIView *rollView;
@property (nonatomic, retain) UIImageView *myImageView;
@property (nonatomic, retain) UIView *headerView;
@property (nonatomic, retain) NSMutableArray *buttonArray;

@end

@implementation RecommendNewMusicViewController
- (void)dealloc
{
//    [_chinese release];
    [self.array release];
    [_rollView release];
    [_myImageView release];
    [_headerView release];
    [_buttonArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.buttonArray = [NSMutableArray array];
    }
    return self;
}



-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];
    
    
    
    [self.myImageView removeFromSuperview];
    
    //    self.navigationItem.title = @"音乐库";
    //    self.navigationController.navigationBar.translucent = NO;
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    //
    self.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    //
    
    NSUserDefaults *aa = [NSUserDefaults standardUserDefaults];
    
    
    if ([aa objectForKey:@"pic1"]) {
        
        //    if (aa) {
        UIImage *image = [UIImage imageNamed:[aa objectForKey:@"pic1"]];
        
        [_myImageView setImage:image];
    } else {
        [_myImageView setImage:[UIImage imageNamed:@"PIC2.png"]];
        
    }
    
    [self.view addSubview:_myImageView];
    [self.view bringSubviewToFront:self.headerView];
    [self.view bringSubviewToFront:self.scrollView];
    [self.view bringSubviewToFront:self.rollView];
    
    
}


- (void)loadView
{
    [super loadView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *exits = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(exitsAction:)];
    self.navigationItem.leftBarButtonItem = exits;
    self.navigationItem.title = @"最新音乐";
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self createView];
    [self createScrollView];
}

#pragma mark - 返回图片的事件
- (void)exitsAction:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 每页都是一个View，View放在scrollView上面
- (void)createView
{
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 667 * 40)];
    self.headerView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_headerView];
    
    NSArray *array = @[@"华语", @"欧美", @"日韩"];
    for (NSInteger i = 0; i < 3; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 3 * i, 0, self.view.frame.size.width / 3, self.view.frame.size.height / 667 * 40)];
        [button setTitle:array[i] forState:UIControlStateNormal];
        button.tag = i + 1;
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_headerView addSubview:button];
        [self.buttonArray addObject:button];
        if (i == 0) {
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    
    [_headerView release];
}

- (void)buttonAction:(id)sender
{

    UIButton *button = (UIButton *)sender;
    [UIView animateWithDuration:0.3 animations:^{
        self.scrollView.contentOffset = CGPointMake(self.view.frame.size.width * (button.tag - 1), 0);
    }];
    [UIView animateWithDuration:0.3 animations:^{
        self.rollView.frame = CGRectMake(self.view.frame.size.width / 3 * (button.tag - 1), self.view.frame.size.height / 667 * 38, self.view.frame.size.width / 3, self.view.frame.size.height / 667 * 2);
    }];
    // 滑动按钮变色
    for (UIButton *button in self.buttonArray) {
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

#pragma mark - 创建大的scrollView
- (void)createScrollView
{
    // 把每个栏目的接口存到一个数组中
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 667 * 40, self.view.frame.size.width, self.view.frame.size.height - self.view.frame.size.height / 667 * 40)];
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3,0);
    
    self.rollView = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 667 * 38, self.view.frame.size.width / 3, self.view.frame.size.height / 667 * 2)];
    [self.rollView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:self.rollView];
    [self.rollView release];
    
    // 循环创建三个controller并把网址传值过去
    for (int i = 0; i < 3; i++) {
        ReNewMusicChineseViewController *chinese = [[ReNewMusicChineseViewController alloc] init];
        [self addChildViewController:chinese];
        chinese.str = self.array[i];
        [_scrollView addSubview:chinese.view];
        chinese.view.frame = CGRectMake(self.view.frame.size.width * i, 0, self.view.frame.size.width, self.view.frame.size.height);
    }
    
    [self.view addSubview:_scrollView];
    

    [_scrollView release];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [UIView animateWithDuration:0.3 animations:^{
        self.rollView.frame = CGRectMake(self.scrollView.contentOffset.x / 3, self.view.frame.size.height / 667 * 38, self.view.frame.size.width / 3, self.view.frame.size.height / 667 * 2);
    }];
}

@end
